<template>
	<view :style="mainStore.themeColor">
		<status-bar title="通知公告" />
		<view class="main">
			<view class="size44 color-222 bold line66">{{ pageData.noticeTitle }}</view>
			<view class="d-flex m-top40 m-bot40">
				<view class="flex-1 size26 color-666">发布人：{{ pageData.createBy }}</view>
				<view class="size26 color-999">{{ pageData.createTime }}</view>
			</view>
			<up-parse :content="pageData.noticeContent" :domain="domain"></up-parse>
		</view>
		<view class="list p-bot30" v-if="list.length">
			<view class="title d-flex a-center">
				<view class="line m-right28"></view>
				<view class="size32 color-333 bold">附件</view>
			</view>
			<view class="item d-flex a-center" v-for="(item,index) in list" :key="index">
				<view class="flex-1 size28 color-333 m-right20">{{ item.name }}</view>
				<view class="btn flexac size24 white" @click="handleDownload(item)">下载</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { domain } from '@/utils/config.js'
	import { useMainStore } from '@/stores/index'
	import { Toast } from '@/utils/utils.js'
	import Http from '@/utils/request.js'
	import statusBar from '../components/statusBar.vue'
	
	const mainStore = useMainStore()
	const pageData = ref('')
	const list = ref([])
	const id = ref('')
	
	onLoad((e) => {
		if(e.id) id.value = e.id
		getInfo()
	})
	
	const getInfo = () => {
		Http.get({
			url: '/hr/getNoticeDetail',
			data: {
				id: id.value
			}
		}).then(res => {
			if(res.code != 0) return Toast(res.msg)
			pageData.value = res.data || ''
			list.value = res.attachment || []
		})
	}

	const handleDownload = (item) => {
		uni.showLoading({
			title: '下载中'
		})
		uni.downloadFile({
			url: item.url,
			success: res => {
				console.log(res)
				let filePath = res.tempFilePath
				let isImg = item.type.startsWith('image')
				if(isImg) {
					// 保存图片到系统相册
					uni.saveImageToPhotosAlbum({
						filePath,
						complete: () => {
							uni.hideLoading()
						}
					})
					return
				}
				// 新开页面打开文档
				uni.openDocument({
					filePath,
					showMenu: true, // 右上角转发分享
					complete: () => {
						uni.hideLoading()
					}
				})
			},
			fail: err => {
				console.log(err)
				uni.hideLoading()
			}
		})
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 50rpx 30rpx;
	}
	.list {
		width: 710rpx;
		background: #F4F5F7;
		border-radius: 20rpx;
		margin: auto;
		.title {
			height: 90rpx;
			border-bottom: 1rpx solid #E1E1E1;
			.line {
				width: 6rpx;
				height: 30rpx;
				background: var(--themeColor);
			}
		}
		.item {
			margin: 30rpx 20rpx 0 30rpx;
			.btn {
				width: 100rpx;
				height: 56rpx;
				background: var(--themeColor);
				border-radius: 28rpx;
			}
		}
	}
</style>
